﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Solution.Business.Component;
using Solution.Business.Entities;
using Solution.Data;
using MvcPaging;

namespace Solution.UI.Web.Controllers
{
    public class MenuSearchController : Controller
    {
        private Solution.Business.Entities.SolutionEntities db = new SolutionEntities();
        private IList<News> list;
        //
        // GET: /Default1/

        private const int DefaultPageSize = 12;
        private IList<News> newsList;

        public ActionResult Index()
        {
            var list = Business.Component.LocationComponent.Location_GetByTop("5", "len([Level]) = 4", "");
            ViewBag.TinhId = new SelectList(list, "LId", "Name");

            var list1 = Business.Component.TransactionComponent.Transaction_GetByTop("50", "len([Level]) = 4", "");
            ViewBag.TrId = new SelectList(list1, "TrId", "Name");

            var list2 = Business.Component.KhungDienTichComponent.KhungDienTich_GetByTop("50","","");
            ViewBag.Khungdientich = new SelectList(list2, "KhungdientichId", "Name");
            return PartialView();
        }


        public ActionResult Search(int? page, FormCollection form, string submitButton)
        {
                //submitButton == "Search";
                List<int> listLocation = new List<int>();
                int location = Int32.Parse(form.GetValues("QuanId").First());
                if (location != 0)
                {
                    listLocation.Add(Int32.Parse(form.GetValues("QuanId").First()));
                }
                else
                {
                    listLocation = (from item in db.Locations
                                    select item.LId).ToList<int>();

                }

                List<int> listTransaction = new List<int>();
                int transaction = Int32.Parse(form.GetValues("Phanloai").First());
                if (transaction != 0)
                {
                    listTransaction.Add(Int32.Parse(form.GetValues("Phanloai").First()));
                    int KhunggiaId = Int32.Parse(form.GetValues("Gia").First());
                    var list2 = Business.Component.KhungGiaComponent.KhungGia_GetByTop("50", "KhunggiaId = '" + KhunggiaId + "'", "");
                    double? a = list2[0].MinPrice;
                    double? b = list2[0].MaxPrice;
                    Session["MinPrice"] = a;
                    Session["MaxPrice"] = b;
                }
                else
                {

                    listTransaction = (from item in db.Transactions
                                       select item.TrId).ToList<int>();
                    Session["MinPrice"] = 0;
                    Session["MaxPrice"] = 100000000;
                }


                int dientich = Int32.Parse(form.GetValues("Khungdientich").First());
                if (dientich != 0)
                {
                    int KhungdientichId = Int32.Parse(form.GetValues("Khungdientich").First());
                    var list3 = Business.Component.KhungDienTichComponent.KhungDienTich_GetByTop("50", "KhungdientichId = '" + KhungdientichId + "'", "");
                    double? c = list3[0].MinValues;
                    double? d = list3[0].MaxValues;
                    Session["MinValues"] = c;
                    Session["MaxValues"] = d;

                }
                else
                {
                    Session["MinValues"] = 0;
                    Session["MaxValues"] = 100000000;
                }

                string txtSearch = form.GetValues("txtSearch") != null ? form.GetValues("txtSearch").First() : "";

                double MinPrice = double.Parse(Session["MinPrice"].ToString());
                double MaxPrice = double.Parse(Session["MaxPrice"].ToString());
                double MinValues = double.Parse(Session["MinValues"].ToString());
                double MaxValues = double.Parse(Session["MaxValues"].ToString());

                var query = from item in db.News
                            where listLocation.Contains(item.LId.Value) &&
                                  listTransaction.Contains(item.TrId.Value) &&
                                  item.Price > MinPrice && item.Price < MaxPrice &&
                                  item.Acreage > MinValues && item.Acreage < MaxValues &&
                                  item.Title.Contains(txtSearch)
                            select item;
                list = query.ToList<News>();
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            return View(this.list.ToPagedList(currentPageIndex, DefaultPageSize));
        }
        
        public ActionResult GetQuanId(int TinhId)
        {
            var list = Business.Component.LocationComponent.Location_GetByTop("50", "len([Level]) > 4 and left([Level], 4) = (select [Level] from Location where LId = '" + TinhId + "')", "");
            return PartialView(list);
        }

        public ActionResult Phanloai(int TrId)
        {
            var list = Business.Component.TransactionComponent.Transaction_GetByTop("50", "len([Level]) >4 and left([Level], 4) = (select [Level] from [Transaction] where TrId = '" + TrId + "')", "");
            return PartialView(list);
        }

        public ActionResult Gia(int TrId)
        {
            var list = Business.Component.KhungGiaComponent.KhungGia_GetByTop("50", "TrId = '" + TrId + "'", "");
            return PartialView(list);

        }
    }
}
